package com.basic.business.common.utils.kmUtil;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.basic.business.common.constants.KmConstants;
import com.basic.business.common.utils.SpringUtils;
import com.basic.business.common.utils.StringUtils;
import com.basic.business.log.service.ITOrdersLogService;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 标签工具类
 *
 * @author zhaoxy_jn@163.com
 */
public class TagUtils {

    /**
     * 标签处理
     * @param sid 系统订单号
     * @param tagIds 标签id
     * @param type 标签类型 1新增 2删除
     * @param logContent 标签操作日志内容
     */
    private static void tagProcessed(long mainSid, long sid, String tagIds, int type, String logContent) {
        Map<String,String> params = new ConcurrentHashMap<>();
        params.put("sids", String.valueOf(sid));
        params.put("tagIds", tagIds);
        params.put("type", String.valueOf(type));
        String result = HttpUtil.post(KmConstants.URL, JSON.toJSONString(PostParamsUtil.getTagParams(KmConstants.TAG_METHOD, params)));
        JSONObject jsonResult = JSONObject.parseObject(result);
        boolean success = jsonResult.getBoolean("success");
        SpringUtils.getBean(ITOrdersLogService.class).insert(mainSid, sid, StringUtils.format("{}{}", logContent, success ? "成功" : "失败"), 0);
    }

    /**
     * 印刷品标签处理
     */
    public static void handleAllPrint(long mainSid, boolean hasNoProcessingTag, boolean hasNormalProcessingTag, boolean hasPrintedTag) {
        if (hasNoProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NO_PROCESSING, 2, "自动处理：删除不做处理标签");
        }
        if (hasNormalProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NORMAL_PROCESSING, 2, "自动处理：删除正常处理标签");
        }
        if (!hasPrintedTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_PRINTED, 1, "自动处理：新增印刷品标签");
        }
    }

    /**
     * 26位编码处理
     */
    public static void handleAll26Code(long mainSid, boolean hasNoProcessingTag, boolean hasNormalProcessingTag, boolean hasPrintedTag) {
        if (!hasNoProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NO_PROCESSING, 1, "自动处理：新增不做处理标签");
        }
        if (hasNormalProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NORMAL_PROCESSING, 2, "自动处理：删除正常处理标签");
        }
        if (hasPrintedTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_PRINTED, 2, "自动处理：删除印刷品标签");
        }
    }

    /**
     * 默认处理
     */
    public static void handleDefaultCase(long mainSid, boolean hasNoProcessingTag, boolean hasNormalProcessingTag, boolean hasPrintedTag) {
        if (hasNoProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NO_PROCESSING, 2, "自动处理：删除不做处理标签");
        }
        if (!hasNormalProcessingTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_NORMAL_PROCESSING, 1, "自动处理：新增正常处理标签");
        }
        if (hasPrintedTag) {
            tagProcessed(mainSid, mainSid, KmConstants.TAG_PRINTED, 2, "自动处理：删除印刷品标签");
        }
    }

}
